(function() {
	angular
		.module('ydApp.doc')
		.config(function($stateProvider) {
			$stateProvider.state('doc-list', {
				url: '/doc-list?keyword?branchId?state',
				controller: "docListCtrl",
				templateUrl: 'views/docs/doc-list.html'
			});
			$stateProvider.state('doc-listselect', {
				url: '/doc-listselect/?state/?branchId',
				controller: "docListSelectCtrl",
				templateUrl: 'views/docs/doc-list.html'
			});
		})
		.controller("docListCtrl", docListCtrl)
		.controller("docListSelectCtrl", docListSelectCtrl);

	function docListCtrl($scope, xhr, $stateParams, $state, localStore) {
		var vm = $scope.vm = {};
		vm.user = localStore.get("user");
		vm.flag = 0;
		vm.conditions = {
			pageIndex: -1,
			condition: "",
			branchId: "",
			sorter: "intelligent",
			userId: vm.user.id
		};

		if ($stateParams.state == 'add-check') {
			vm.conditions.auditState = 0;
		}

		vm.conditions.branchId = $stateParams.branchId;
		vm.conditions.condition = $stateParams.keyword;

		vm.menu = function(id) {
			if (vm.flag == id) {
				vm.flag = 0;
			} else {
				vm.flag = id;
			}
		}
		vm.departSearch = function(branchId) {
			vm.conditions.branchId = branchId || undefined;
			vm.flag = 0;
			vm.doRefresh();
		}
		vm.sort = function(name, desc) {
			vm.conditions.sorter = name;
			vm.flag = 0;
			vm.doRefresh();
		}
		vm.keywordSearch = function(event) {
			if (event.keyCode == 13) {
				vm.doRefresh();
			}
		};
		xhr.get("HealthInquiry/com/branch/findByCondition", {
			pageIndex: 0,
			pageSize: 100
		}).then(function(res) {
			if (res.successful) {
				vm.departs = res.data;
			}
		})
		vm.docs = [];
		vm.nodata = false;

		vm.doRefresh = function() {
			vm.docs = [];
			vm.conditions.pageIndex = 0;
			vm.nodata = false;
			vm.load().finally(function() {
				$scope.$broadcast('scroll.refreshComplete');
			});
		}
		vm.loadMore = function() {
			vm.conditions.pageIndex++;
			vm.load().then(function() {
				$scope.$broadcast('scroll.infiniteScrollComplete');
			});
		}
		vm.load = function() {
			return xhr.get("HealthInquiry/com/doctor/queryDoctor", vm.conditions).then(function(docs) {
				if (docs.data.length == 0) {
					vm.nodata = true;
				}
				vm.docs = vm.docs.concat(docs.data);
			})
		}
		vm.toDetail = function(docId, docName) {
			var state = $stateParams.state;
			if (state) {
				var user = localStore.get("user");
				user.docName = docName;
				user.docId = docId;
				localStore.save("user", user);
				$state.go(state);
			} else {
				$state.go("doc-detail", {
					docId: docId
				});
			}

		}
	}

	function docListSelectCtrl($window, $scope, xhr, $stateParams, $state, localStore) {
		var vm = $scope.vm = {};
		vm.user = localStore.get("user");
		vm.flag = 0;
		vm.conditions = {
			pageIndex: -1,
			auditState: 1,
			appraisalState: 1,
			branchId: 0,
			pageSize: 50,
			sorter: ""
		};

		vm.conditions.branchId = $stateParams.branchId;
		vm.conditions.condition = $stateParams.keyword;

		vm.menu = function(id) {
			if (vm.flag == id) {
				vm.flag = 0;
			} else {
				vm.flag = id;
			}
		}
		vm.departSearch = function(branchId) {
			vm.conditions.branchId = branchId || undefined;
			vm.flag = 0;
			vm.doRefresh();
		}
		vm.sort = function(name, desc) {
			vm.conditions.sorter = name;
			if (name == 'intelligent') {
				vm.conditions.sorter = ''
			}
			vm.flag = 0;
			vm.doRefresh();
		}
		vm.keywordSearch = function(event) {
			if (event.keyCode == 13) {
				vm.doRefresh();
			}
		};
		xhr.get("HealthInquiry/com/branch/findByCondition", {
			pageIndex: 0,
			pageSize: 100
		}).then(function(res) {
			if (res.successful) {
				vm.departs = res.data;
			}
		})
		vm.docs = [];
		vm.nodata = false;

		vm.doRefresh = function() {
			vm.docs = [];
			vm.conditions.pageIndex = 0;
			vm.nodata = false;
			vm.load().finally(function() {
				$scope.$broadcast('scroll.refreshComplete');
			});
		}
		vm.loadMore = function() {
			vm.conditions.pageIndex++;
			vm.load().then(function() {
				$scope.$broadcast('scroll.infiniteScrollComplete');
			});
		}
		vm.load = function() {
			return xhr.get("HealthInquiry/com/doctor/findByCondition", vm.conditions).then(function(docs) {
				if (docs.data.length == 0) {
					vm.nodata = true;
				}
				vm.docs = vm.docs.concat(docs.data);
			})
		}
		vm.toDetail = function(docId, docName) {
			var state = $stateParams.state;
			if (state) {
				var user = localStore.get("user");
				user.docName = docName;
				user.docId = docId;
				localStore.save("user", user);
				if (angular.equals($stateParams.state, "add-check")) {
					$window.history.back();
				} else {
					$state.go(state);
				}
			} else {
				$state.go("doc-detail", {
					docId: docId
				});
			}

		}
	}
})();